package org.compiere.model;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Properties;
import java.util.logging.Level;

import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.Env;

public class Mhrmmedicarelinelist extends X_hrm_medicare_line_list {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4645489486237996709L;
	
	/**	Static Logger	*/
	private static CLogger	s_log	= CLogger.getCLogger (Mhrmmedicarelinelist.class);

	public Mhrmmedicarelinelist(Properties ctx) {
		super(ctx);
	
	}
	
	public Mhrmmedicarelinelist(Properties ctx, int hrm_medicare_line_list_ID,
			String trxName) {
		super(ctx, hrm_medicare_line_list_ID, trxName);
	
	}
	
	public Mhrmmedicarelinelist(Properties ctx, ResultSet rs, String trxName) {
		super(ctx, rs, trxName);
		
	}
	
public static Mhrmmedicarelinelist getMedicareList(int C_BPartner_ID, Timestamp dt, int medicareLineID) {
		
		//
	    PreparedStatement pstmt = null;
		ResultSet rs = null;
		Mhrmmedicarelinelist medicarelist = null;
			
		// 
			String sql_ = "SELECT * FROM HRM_MEDICARE_LINE_LIST \n " +
							"WHERE ISACTIVE='Y' \n " +
							"AND AD_CLIENT_ID=" + Env.getAD_Client_ID(Env.getCtx()) + " \n " + 
							"AND C_BPARTNER_ID=" + C_BPartner_ID + " \n " +
							"AND HRM_Medicare_Line_ID=" + medicareLineID + " \n " +
							"AND TO_DATE('01-'||TO_CHAR(DATEFROM,'MM-YYYY'),'DD-MM-YYYY') <=" + DB.TO_DATE(dt) + " \n " +
							"AND TO_DATE('28-'||TO_CHAR(DATEFROM,'MM-YYYY'),'DD-MM-YYYY') >=" + DB.TO_DATE(dt);
			try
			{
				pstmt = DB.prepareStatement(sql_,null);				
				rs = pstmt.executeQuery();	
				if(rs.next())
					medicarelist = new Mhrmmedicarelinelist(Env.getCtx(), rs, null);
			}
			catch (SQLException e)
			{
				s_log.log(Level.SEVERE, "product", e);
			}
			finally
			{
				DB.close(rs, pstmt);
				rs = null; pstmt = null;
			}
			
		return medicarelist;
		
	}

public static Mhrmmedicarelinelist getMedicareRec(int hrm_mediacare_line_id, int C_BPartner_ID) {
	
	//
    PreparedStatement pstmt = null;
	ResultSet rs = null;
	Mhrmmedicarelinelist medicarelist = null;
		
	// 
		String sql_ = "SELECT * FROM HRM_MEDICARE_LINE_LIST \n " +
						"WHERE ISACTIVE='Y' \n " +
						"AND AD_CLIENT_ID=" + Env.getAD_Client_ID(Env.getCtx()) + " \n " +
						"AND HRM_MEDICARE_LINE_ID=" + hrm_mediacare_line_id + " \n " +
						"AND C_BPARTNER_ID=" + C_BPartner_ID;
		try
		{
			pstmt = DB.prepareStatement(sql_,null);			
			rs = pstmt.executeQuery();	
			if(rs.next())
				medicarelist = new Mhrmmedicarelinelist(Env.getCtx(), rs, null);
		}
		catch (SQLException e)
		{
			s_log.log(Level.SEVERE, "product", e);
		}
		finally
		{
			DB.close(rs, pstmt);
			rs = null; pstmt = null;
		}
		
	return medicarelist;
	
	}
}
